package com.mall.service.impl;


import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mall.mapper.ShoppingCartMapper;
import com.mall.pojo.OrderDetail;
import com.mall.pojo.ShoppingCart;
import com.mall.service.IShoppingCartService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author xiao-chen
 * @since 2023-04-27
 */
@Service
public class ShoppingCartServiceImpl extends ServiceImpl<ShoppingCartMapper, ShoppingCart> implements IShoppingCartService {
    @Resource
    private ShoppingCartMapper shoppingCartMapper;

    @Override
    public void updateShoppingCartWaresNum(Long userId, List<OrderDetail> orderDetailList) {
        orderDetailList.forEach(item -> {
            Integer num = shoppingCartMapper.selectShoppingCartWaresNum(userId, item.getWaresId());
            Integer wareOrderNum = item.getNum();
            if (num.equals(wareOrderNum)) {
                shoppingCartMapper.deleteShoppingCartWares(userId, item.getWaresId());
            } else {
                shoppingCartMapper.updateShoppingCartWaresNum(userId, item.getWaresId(), num - wareOrderNum);
            }
        });

    }
}
